/*
   Author: mahaiyuan
   Date: 2021/9/6 3:06 下午
   Description: 用于记录接口的输入、输出、耗时等相关内容
*/
package middleware

import (
	"github.com/gin-gonic/gin"
	"go-web/pkg/utils"
	"go-web/pkg/zlog"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"time"
)


/*
{
    "level":"INFO",
    "time":"2021-09-06 14:08:11.457523",
    "file":"middleware/accesslog.go:163",
    "msg":"notice",
    "logId":"0485316338",
    "requestId":"b5650f48eae99a53:23a269a9db05f909:b5650f48eae99a53:1",
    "module":"kplight",
    "localIp":"172.26.65.214",
    "uri":"/kplight/api/friend/list",
    "cuid":"",
    "device":"",
    "channel":"",
    "os":"",
    "vc":"",
    "vcname":"",
    "userid":"",
    "host":"kplight-svc.kunpeng",
    "method":"POST",
    "httpProto":"HTTP/1.1",
    "handle":"kplight/controllers/http/friend.List",
    "userAgent":"RAL/2.0.26.1 (internal request)",
    "refer":"/assistantdesk/",
    "clientIp":"172.27.131.242",
    "cookie":"",
    "requestStartTime":"1630908485.503421",
    "requestEndTime":"1630908491.457393",
    "cost":5953.97,
    "requestParam":"userId=zybwinxx&corpId=ww82a47fd1d43f9bbb&ability=0&friendSign=1&timeStamp=1630908485&from=kpstaff&fromSign=9414151901269623CCB8E71731EC0820",
    "responseStatus":200,
    "response":"{",
    "globalCustomerNotice":"kplight"
}
 */

func AccessLog() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		start := time.Now()
		ctx.Next()
		end := time.Now()
		fields := []zapcore.Field{
			//zap.String("cuid", getReqValueByKey(c, "cuid")),
			//zap.String("device", getReqValueByKey(c, "device")),
			//zap.String("channel", getReqValueByKey(c, "channel")),
			//zap.String("os", getReqValueByKey(c, "os")),
			//zap.String("vc", getReqValueByKey(c, "vc")),
			//zap.String("vcname", getReqValueByKey(c, "vcname")),
			//zap.String("userid", getReqValueByKey(c, "userid")),
			zap.String("uri", ctx.Request.RequestURI),
			zap.String("host", ctx.Request.Host),
			zap.String("method", ctx.Request.Method),
			zap.String("httpProto", ctx.Request.Proto),
			zap.String("handle", ctx.HandlerName()),
			zap.String("userAgent", ctx.Request.UserAgent()),
			zap.String("refer", ctx.Request.Referer()),
			//zap.String("clientIp", utils.GetClientIp(c)),
			//zap.String("cookie", getCookie(c)),
			//zap.String("requestStartTime", utils.GetFormatRequestTime(start)),
			//zap.String("requestEndTime", utils.GetFormatRequestTime(end)),
			zap.Float64("cost", utils.GetCostTime(start, end)),
			//zap.String("requestParam", bodyStr),
			zap.Int("responseStatus", ctx.Writer.Status()),
			//zap.String("response", response),
		}
		zlog.Infow("notice", fields)
	}
}